package vn.isolar.object;

import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.widget.Toast;
import android.app.Activity;

public class Command extends Activity {
	String cmd; // String command
	
	/*
	 * Type of command
	 * 1 - Direction
	 * 2 - Location
	 * 3 - SMS
	 * 4 - Set alarm, calendar 
	 */
	private int type; 	
	
	/*
	 * Declare variable for Direction command
	 */
	
	private String srcAddr; 	//Source address
	private String desAddr;		//Destination address
	//----------------------------------------
	
	/*
	 * Declare variable for Location
	 * none
	 */
	//private LocationManager locManager;		//Current Location Manager
	//----------------------------------------
	
	/*
	 * Declare variable for SMS
	 */
	private String namePerson; 	// Name of reciver
	private String bodySMS;		// Body of SMS
	
	/*
	 * Decalre variable for Alarm, Calendar
	 */
	private int hourAlarm;
	private int minuteAlarm;
	
	public Command(String cmd){
		this.cmd = cmd;
		this.parseCmd();
	}
	
	public int getType(){
		return this.type;
	}
	/*
	 * Text analyze here
	 */
	public void parseCmd(){
		/*
		 * Analyze Return String
		 */
		//String result	=	"1/1/Thuy Khue, Ha Noi/ Hai Ba Trung, Ha Noi";
		//String result	=	"1/2";
		String result	=	cmd;
		//String result	=	"1/3/01678696869/Di bay em oi";
		//String result	=	"1/4/23/25";
		String[] resultArr = result.split("/");
		
		type = Integer.parseInt(resultArr[1]);
		
		// Set Direction Values
		if (type == 1) {
			srcAddr	=	resultArr[2];
			desAddr	=	resultArr[3];
		}
		// Set SMS Values
		if (type == 3) {
			namePerson	=	resultArr[2];
			bodySMS		=	resultArr[3];
		}
		// Set Alarm Values
		if (type == 4) {
			hourAlarm	=	Integer.parseInt(resultArr[2]);
			minuteAlarm	=	Integer.parseInt(resultArr[3]);
		}
	} 
	public Executable createExe(){
		switch (type){
			case 2:			// If command is get location
				return new Executable("GET_LOCATION");
				
			default:		// If command is direction,sms,alarm
				return new Executable(createIntent());
				
		}
		
	}
	
	public Intent createIntent(){
		Intent intent = null;
		switch(type){
			case 1:			// Direction
				intent = new Intent(android.content.Intent.ACTION_VIEW, 
						Uri.parse("http://maps.google.com/maps?saddr="+srcAddr+"&daddr="+desAddr));
				intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
				break;
			case 3:			// SMS
			    intent = new Intent(Intent.ACTION_SENDTO,
			    		Uri.parse("smsto:" + namePerson));
			    intent.putExtra("sms_body", bodySMS);
				break;
			case 4:			// Set Alarm
				intent = new Intent(AlarmClock.ACTION_SET_ALARM);
				intent.putExtra(AlarmClock.EXTRA_HOUR, hourAlarm);
				intent.putExtra(AlarmClock.EXTRA_MINUTES, minuteAlarm);
				break;
			
		}
		return intent;
	}
	public class MyLocationListener implements LocationListener
    {

      public void onLocationChanged(Location loc)
      {

        loc.getLatitude();
        loc.getLongitude();

        String Text = "My current location is: " +
        "Latitud = " + loc.getLatitude() +
        "Longitud = " + loc.getLongitude();

        Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
      }

      public void onProviderDisabled(String provider)
      {
        Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
      }

      public void onProviderEnabled(String provider)
      {
        Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
      }

      public void onStatusChanged(String provider, int status, Bundle extras)
      {

      }
    }


}
